/*
 * main.cpp
 * 
 * Copyright 2011 Andrew Tselischev <andrewts@thinkpad-gentoo-89r>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SeveralStacks.h"

#define BUFSIZE 64
#define UNIQ_PREF_LEN 2

const char *cmdlist[] = {
	"init",
	"push",
	"pop",
	"top",
	"isempty"
};

int
main(int argc, char **argv)
{
	static SeveralStacks ss;
	char line[BUFSIZE];
	char cmd[12];
	int stack_num;
	int key;

	while (fgets(line, BUFSIZE - 1, stdin)) {
		if (!strncmp(line, cmdlist[0], UNIQ_PREF_LEN))
		{
			Init(&ss);
		}
		else if (!strncmp(line, cmdlist[1], UNIQ_PREF_LEN))
		{
			(void) sscanf(&line[strlen(cmdlist[1]) + 1], "%d %d", &stack_num, &key);
			Push(&ss, stack_num, key);
		}
		else if (!strncmp(line, cmdlist[2], UNIQ_PREF_LEN))
		{
			(void) sscanf(&line[strlen(cmdlist[2])+1], "%d", &stack_num);
			(void) printf("-> %d\n", Pop(&ss, stack_num));
		}
		else if (!strncmp(line, cmdlist[3], UNIQ_PREF_LEN))
		{
			(void) sscanf(&line[strlen(cmdlist[3])+1], "%d", &stack_num);
			(void) printf("-> %d\n", Top(&ss, stack_num));
		}
		else if (!strncmp(line, cmdlist[4], UNIQ_PREF_LEN))
		{
			(void) sscanf(&line[strlen(cmdlist[4])+1], "%d", &stack_num);
			(void) printf("-> %s\n", IsEmpty(&ss, stack_num) ? "empty" : "not empty");
		} else
			(void) printf("-! unknown command \"%s\"\n", line);
	}
	return 0;
}
